﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>添加Popup标注</title>
    <link href="../../libs/ol/ol.css" rel="stylesheet" type="text/css" />
    <script src="../../libs/ol/ol.js" type="text/javascript"></script>
<!--    <script src="../../libs/ol/ol-debug.js" type="text/javascript"></script>-->
    <!--  引入第三方插件库 -->
    <script src="../../libs/jquery-1.11.2.min.js" type="text/javascript"></script>
    <style type="text/css">
        body,html{
            border:none;padding:0;margin:0;
        }
        #menu{
            width:100%;
            height:20px;         
            padding:5px 10px;
            font-size:14px;
            font-family:"微软雅黑";
            left:10px;    
        }
        #map{
            width:100%;
            height:570px;
            position: relative;
        }
        .ol-popup {
          position: absolute;
          background-color: white;
          -webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
          filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
          padding: 15px;
          border-radius: 10px;
          border: 1px solid #cccccc;
          bottom: 12px;
          left: -50px;
        }
        .ol-popup:after, .ol-popup:before {
          top: 100%;
          border: solid transparent;
          content: " ";
          height: 0;
          width: 0;
          position: absolute;
          pointer-events: none;
        }
        .ol-popup:after {
          border-top-color: white;
          border-width: 10px;
          left: 48px;
          margin-left: -10px;
        }
        .ol-popup:before {
          border-top-color: #cccccc;
          border-width: 11px;
          left: 48px;
          margin-left: -11px;
        }
        .ol-popup-closer {
          text-decoration: none;
          position: absolute;
          top: 2px;
          right: 8px;
        }
        .ol-popup-closer:after {
          content: "✖";
        }
        #popup-content{
            font-size:14px;
            font-family:"微软雅黑";
        }
        #popup-content .markerInfo {
            font-weight:bold; 
        }
    </style>
</head>
<body>
    <div id="menu">
        鼠标单击标注点弹出Popup标注
    </div> 
    <div id="map" > 
        <!-- Popup -->
        <div id="popup" class="ol-popup" >
              <a href="#" id="popup-closer" class="ol-popup-closer"></a>
              <div id="popup-content">          
              </div>
        </div>
    </div>
    <script type="text/javascript">
        var beijing = ol.proj.fromLonLat([116.28, 39.54]);
        //示例标注点北京市的信息对象
        var featuerInfo = {
            geo: beijing,
            att: {
                title: "北京市(中华人民共和国首都)", //标注信息的标题内容
                titleURL: "http://www.openlayers.org/", //标注详细信息链接
                text: "北京（Beijing），简称京，中华人民共和国首都、直辖市，中国的政治、文化和国际交往中心……", //标注内容简介
                imgURL: "../../images/label/bj.png" //标注的图片
            }
        }

        /**
        * 实例化Map对象加载地图,默认底图加载MapQuest地图
        */
        var map = new ol.Map({
            layers: [
                new ol.layer.Tile({
                    source: new ol.source.OSM()
                })
              ],
            target: 'map',
            view: new ol.View({
                center: beijing,
                zoom: 3
            })
        });

        /**
        * 创建标注样式函数,设置image为图标ol.style.Icon
        * @param {ol.Feature} feature 要素
        */
        var createLabelStyle = function (feature) {
            return new ol.style.Style({
                image: new ol.style.Icon(/** @type {olx.style.IconOptions} */({
                    anchor: [0.5, 60],
                    anchorOrigin: 'top-right',
                    anchorXUnits: 'fraction',
                    anchorYUnits: 'pixels',
                    offsetOrigin: 'top-right',
                    // offset:[0,10],
                    // scale:0.5,  //图标缩放比例
                    opacity: 0.75,  //透明度
                    src: '../../images/label/blueIcon.png' //图标的url
                })),
                text: new ol.style.Text({
                    textAlign: 'center', //位置
                    textBaseline: 'middle', //基准线
                    font: 'normal 14px 微软雅黑',  //文字样式
                    text: feature.get('name'),  //文本内容
                    fill: new ol.style.Fill({ color: '#aa3300' }), //文本填充样式（即文字颜色）
                    stroke: new ol.style.Stroke({ color: '#ffcc33', width: 2 })
                })
            });
        }

        //实例化Vector要素，通过矢量图层添加到地图容器中
        var iconFeature = new ol.Feature({
            geometry: new ol.geom.Point(beijing),
            name: '北京市',  //名称属性
            population: 2115 //大概人口数（万）
        });
        iconFeature.setStyle(createLabelStyle(iconFeature));
        //矢量标注的数据源
        var vectorSource = new ol.source.Vector({
            features: [iconFeature]
        });
        //矢量标注图层
        var vectorLayer = new ol.layer.Vector({
            source: vectorSource
        });
        map.addLayer(vectorLayer);

        /**
        * 实现popup的html元素
        */
        var container = document.getElementById('popup');
        var content = document.getElementById('popup-content');
        var closer = document.getElementById('popup-closer');

        /**
        * 在地图容器中创建一个Overlay
        */
        var popup = new ol.Overlay(/** @type {olx.OverlayOptions} */({
            element: container,
            autoPan: true,
            positioning: 'bottom-center',
            stopEvent: false,
            autoPanAnimation: {
                duration: 250
            }
        }));
        map.addOverlay(popup);

        /**
        * 添加关闭按钮的单击事件（隐藏popup）
        * @return {boolean} Don't follow the href.
        */
        closer.onclick = function () {
            popup.setPosition(undefined);  //未定义popup位置
            closer.blur(); //失去焦点
            return false;
        };

        /**
        * 动态创建popup的具体内容
        * @param {string} title 
        */
        function addFeatrueInfo(info) {        
            //新增a元素
            var elementA = document.createElement('a');
            elementA.className = "markerInfo";
            elementA.href = info.att.titleURL;
            //elementA.innerText = info.att.title;
            setInnerText(elementA, info.att.title);
            content.appendChild(elementA); // 新建的div元素添加a子节点
            //新增div元素
            var elementDiv = document.createElement('div');
            elementDiv.className = "markerText";
            //elementDiv.innerText = info.att.text;
            setInnerText(elementDiv, info.att.text);
            content.appendChild(elementDiv); // 为content添加div子节点
            //新增img元素
            var elementImg = document.createElement('img');
            elementImg.className = "markerImg";
            elementImg.src = info.att.imgURL;
            content.appendChild(elementImg); // 为content添加img子节点          
        }
        /**
        * 动态设置元素文本内容（兼容）
        */
        function setInnerText(element, text) {
            if (typeof element.textContent == "string") {
                element.textContent = text;
            } else {
                element.innerText = text;
            }
        }

        /**
        * 为map添加点击事件监听，渲染弹出popup
        */
        map.on('click', function (evt) {
            var coordinate = evt.coordinate;
            //判断当前单击处是否有要素，捕获到要素时弹出popup
            var feature = map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) { return feature; });
            if (feature) {
                content.innerHTML = ''; //清空popup的内容容器
                addFeatrueInfo(featuerInfo); //在popup中加载当前要素的具体信息
                if (popup.getPosition() == undefined) {
                    popup.setPosition(coordinate); //设置popup的位置
                }
            }
        });
        /**
        * 为map添加鼠标移动事件监听，当指向标注时改变鼠标光标状态
        */
        map.on('pointermove', function (e) {
            var pixel = map.getEventPixel(e.originalEvent);
            var hit = map.hasFeatureAtPixel(pixel);
            map.getTargetElement().style.cursor = hit ? 'pointer' : '';
        });
    </script>
</body>
</html>
